package com.hctg.forced2sleep.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.actor.myandroidframework.utils.LogUtils;
import com.hctg.forced2sleep.databinding.ActivityVideoDetailBinding;

/**
 * description: 播放视频
 * company    :
 * @author    : ldf
 * date       : 2024/6/17 on 16:17
 */
public class VideoDetailActivity extends BaseActivity<ActivityVideoDetailBinding> {

    /**
     * @param title 标题
     * @param introduction 介绍
     * @param videoPath 路径
     */
    public static void start(Context context, String title, String introduction, String videoPath) {
        context.startActivity(new Intent(context, VideoDetailActivity.class)
                .putExtra("title", title)
                .putExtra("introduction", introduction)
                .putExtra("videoPath", videoPath)
        );
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String introduction = intent.getStringExtra("introduction");
        String videoPath = intent.getStringExtra("videoPath");

        viewBinding.titleBar.setTitle(title);
        viewBinding.tvIntroduction.setText(introduction);
        viewBinding.videoView.setVideoPath(videoPath);
        //返回
        viewBinding.titleBar.ivBack.setOnClickListener(v -> {
            finish();
        });
        viewBinding.videoView.setOnClickListener((View.OnClickListener) v -> {
            LogUtils.errorFormat("isPlaying() = %b", viewBinding.videoView.isPlaying());
            if (viewBinding.videoView.isPlaying()) {
                viewBinding.videoView.pause();
                viewBinding.ivStopIcon.setVisibility(View.VISIBLE);
            } else {
                viewBinding.videoView.start();
                viewBinding.ivStopIcon.setVisibility(View.INVISIBLE);
            }
        });
        //准备监听
//        viewBinding.videoView.setOnPreparedListener(mp -> {
//        });
        viewBinding.videoView.setOnErrorListener((mp, what, extra) -> {
            return false;
        });
        //播放完成
        viewBinding.videoView.setOnCompletionListener(mp -> {
            viewBinding.ivStopIcon.setVisibility(View.VISIBLE);
        });
        //暂停
        viewBinding.ivStopIcon.setOnClickListener(v -> {
            v.setVisibility(View.INVISIBLE);
            viewBinding.videoView.start();
        });
        viewBinding.videoView.start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        viewBinding.videoView.resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        viewBinding.videoView.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        viewBinding.videoView.stopPlayback();
        viewBinding.videoView.suspend();
    }
}